package org.jeecg.modules.wangyue.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wangyue.constant.DelFlagConstant;
import org.jeecg.modules.wangyue.constant.MyInfoConstant;
import org.jeecg.modules.wangyue.constant.ProblemFeedbackConstant;
import org.jeecg.modules.wangyue.entity.MyInfo;
import org.jeecg.modules.wangyue.entity.ProblemFeedback;
import org.jeecg.modules.wangyue.mapper.MyInfoMapper;
import org.jeecg.modules.wangyue.mapper.ProblemFeedbackMapper;
import org.jeecg.modules.wangyue.service.IProblemFeedbackService;
import org.jeecg.modules.wangyue.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 问题反馈表
 * @Author: jeecg-boot
 * @Date:   2023-06-30
 * @Version: V1.0
 */
@Service
public class ProblemFeedbackServiceImpl extends ServiceImpl<ProblemFeedbackMapper, ProblemFeedback> implements IProblemFeedbackService {

    @Autowired
    private ProblemFeedbackMapper problemFeedbackMapper;
    @Autowired
    private MyInfoMapper myInfoMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> handleProblem(ProblemFeedback problemFeedback) {
        String id = problemFeedback.getId();        //问题反馈表唯一id
        String answerId = problemFeedback.getAnswerId();    //回复人id
        String answerContent = problemFeedback.getAnswerContent();  //回复内容
        if(oConvertUtils.isEmpty(id)){
            return Result.error("id不能为空");
        }
        if(oConvertUtils.isEmpty(answerId)){
            return Result.error("回复人id不能为空");
        }
        if(oConvertUtils.isEmpty(answerContent)){
            return Result.error("回复内容不能为空");
        }

        //1.将问题反馈的“待处理”变为“已处理”
        UpdateWrapper<ProblemFeedback> problemFeedbackUpdateWrapper = new UpdateWrapper<>();
        problemFeedbackUpdateWrapper.eq("id", id);
        problemFeedbackUpdateWrapper.eq("fb_status", ProblemFeedbackConstant.fbStatus_wait); //待处理(待回复)
        problemFeedbackUpdateWrapper.eq("del_flag", DelFlagConstant.normal);
        problemFeedbackUpdateWrapper.set("answer_id", answerId);
        problemFeedbackUpdateWrapper.set("answer_content", answerContent);
        problemFeedbackUpdateWrapper.set("fb_status", ProblemFeedbackConstant.fbStatus_complete); //已处理(已回复)
        problemFeedbackUpdateWrapper.set("update_time", DateUtil.getTime("yyyy-MM-dd HH:mm:ss"));
        int problemFeedbackNum = problemFeedbackMapper.update(null, problemFeedbackUpdateWrapper);
        if(problemFeedbackNum < 1){
            throw new RuntimeException("问题反馈处理-失败(1)");
        }

        //2.通知技师有问题反馈已处理
        //查询问题反馈信息
        ProblemFeedback pf = problemFeedbackMapper.selectById(id);
        MyInfo myInfo = new MyInfo();
        myInfo.setTitle("您有一个问题反馈已处理");
        myInfo.setContent("问题：" + pf.getFbContent());
        myInfo.setUserId(pf.getFbUserId());
        myInfo.setUserType(pf.getFbUserType());
        myInfo.setInfoType(MyInfoConstant.infoType_feedback);
        int MyInfoNum = myInfoMapper.insert(myInfo);
        if(MyInfoNum < 1){
            throw new RuntimeException("问题反馈处理-失败(2)");
        }

        return Result.OK("问题反馈处理成功");
    }



}
